//
//  UIImage+QR.m
//  HSY
//
//  Created by 陈浩 on 2017/4/20.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "UIImage+QR.h"
#import <Photos/Photos.h>

@implementation UIImage (QR)

+ (UIImage *)qrImageFromString:(NSString *)string{
    if (!string) return nil;
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜inputMessage数据
    [filter setValue:data forKeyPath:@"inputMessage"];
    // 4.获取输出的二维码
    CIImage *outputImage = [filter outputImage];
    // 5.将CIImage转换成UIImage，并放大显示
    
    UIImage *emptyCardImage = [UIImage imageNamed:@"qr_card_1"];
    CGSize size = emptyCardImage.size;
    
//    return [UIImage imageWithCIImage:outputImage scale:20.0 orientation:UIImageOrientationUp];
    return [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:150/278.0*size.width];

}

+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

- (UIImage *)getImageFromView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)cardImageWithQrImage:(UIImage *)qrImage qrCode:(NSString *)qrCode{
    if ((!qrImage) && (!qrCode)) return nil;
    
    if (!qrImage) {
        qrImage = [self qrImageFromString:qrCode];
    }
    
    UIImage *emptyCardImage = [UIImage imageNamed:@"qr_card_1"];
    
    CGSize size = emptyCardImage.size;
    
    UIGraphicsBeginImageContext(size);
    [emptyCardImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    [qrImage drawInRect:CGRectMake(64/278.0*size.width, 90/394.0*size.height, 150/278.0*size.width, 150/394.0*size.height)];
    UIImage *togetherImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return togetherImage;

}

@end
